package com.ms.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ms.annotation.AdminOrOrganizerOnly;
import com.ms.entity.PlaceRecord;
import com.ms.service.inter.PlaceRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 场地记录控制器
 * 处理场地记录相关的HTTP请求
 */
@RestController
@RequestMapping("/placeRecord")
public class PlaceRecordController {

    @Autowired
    private PlaceRecordService placeRecordService;

    /**
     * 根据场地ID获取场地记录列表
     * 该方法供管理员和主办方访问
     *
     * @param placeId 场地ID
     * @return 包含场地记录列表的响应结果
     */
    //测试路径：localhost:8080/placeRecord/listByPlace?placeId=1
    @AdminOrOrganizerOnly
    @GetMapping("/listByPlace")
    public Map<String, Object> getPlaceRecordsByPlaceId(@RequestParam("placeId") Integer placeId) {
        Map<String, Object> result = new HashMap<>();

        try {
            // 使用QueryWrapper查询PlaceRecord表中pl_id等于placeId的记录
            QueryWrapper<PlaceRecord> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("pl_id", placeId);
            List<PlaceRecord> placeRecords = placeRecordService.list(queryWrapper);

            result.put("success", true);
            result.put("data", placeRecords);
            result.put("msg", "获取成功");
        } catch (Exception e) {
            result.put("success", false);
            result.put("msg", "获取失败: " + e.getMessage());
        }

        return result;
    }

    /**
     * 根据租借方ID获取场地记录列表
     * 该方法供管理员和主办方访问
     *
     * @param organizerId 租借方ID
     * @return 包含场地记录列表的响应结果
     */
    @AdminOrOrganizerOnly
    @GetMapping("/listByOrganizer")
    public Map<String, Object> getPlaceRecordsByOrganizerId(@RequestParam("organizerId") Integer organizerId) {
        Map<String, Object> result = new HashMap<>();

        try {
            // 使用QueryWrapper查询PlaceRecord表中or_id等于organizerId的记录
            QueryWrapper<PlaceRecord> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("or_id", organizerId);
            List<PlaceRecord> placeRecords = placeRecordService.list(queryWrapper);

            result.put("success", true);
            result.put("data", placeRecords);
            result.put("msg", "获取成功");
        } catch (Exception e) {
            result.put("success", false);
            result.put("msg", "获取失败: " + e.getMessage());
        }

        return result;
    }

    /**
     * 获取所有场地记录列表
     * 该方法供管理员和主办方访问
     *
     * @return 包含所有场地记录列表的响应结果
     */
    @AdminOrOrganizerOnly
    @GetMapping("/listAll")
    public Map<String, Object> getAllPlaceRecords() {
        Map<String, Object> result = new HashMap<>();

        try {
            // 查询所有场地记录
            List<PlaceRecord> placeRecords = placeRecordService.list();

            result.put("success", true);
            result.put("data", placeRecords);
            result.put("msg", "获取成功");
        } catch (Exception e) {
            result.put("success", false);
            result.put("msg", "获取失败: " + e.getMessage());
        }

        return result;
    }
}